import { Component, OnInit } from '@angular/core';
import { ProgramService } from 'src/app/core/service/program.service';
import { NzModalService } from 'ng-zorro-antd';
import { AddProjectComponent } from './add-project/add-project.component';
import { AddAppComponent } from './add-app/add-app.component';
import { ProjectService } from 'src/app/core/service/project.service';

@Component({
  selector: 'app-project-list',
  templateUrl: './project-list.component.html',
  styleUrls: ['./project-list.component.scss']
})
export class ProjectListComponent implements OnInit {
  allChecked = false;
  indeterminate = false;
  displayData = [];
  overviewProData = {};
  overviewProMark = ''; // 项目页刷新
  pageIndex = 1;      // 页码标识
  pageSize = 10;      // 每页展示多少数据 —— 10条
  total = 1;          // 总共1页

  dataSet = [];       // 数据集合

  loading = true;     // 加载
  sortValue = null;   // 种类值
  sortKey = null;     // 种类关键值
  searchGenderList: string[] = [];

  sort(sort: { key: string, value: string }): void {
    this.sortKey = sort.key;
    this.sortValue = sort.value;
  }

  constructor(
    private programService: ProgramService,
    private modalService: NzModalService,
    private projectService: ProjectService
  ) { }

  /**
   * 获取项目列表信息
   * @param reset 获取数据
   */
  getProgramPage(reset: boolean = false): void {
    console.log('=== getProgramPage() ===>coming in...');
    if (reset) {
      this.pageIndex = 1;
    }
    this.programService.getProgramPage(this.pageIndex, this.pageSize)
      .subscribe((programPage) => {
        // console.log(`=== getProgramPage().programPage ===> ${programPage}`);
        this.loading = false;
        this.dataSet = programPage.data;
        this.total = programPage.total;
        // console.log('=== getProgramPage().programPage.total ===>', programPage.total);
      });
  }

  updateFilter(value: string[]): void {
    this.searchGenderList = value;
  }

  currentPageDataChange($event: Array<{ name: string; age: number; address: string; checked: boolean; disabled: boolean; }>): void {
    this.displayData = $event;
    this.refreshStatus();
  }


  /**
   * 刷新状态
   */
  refreshStatus(): void {
    const allChecked = this.displayData.filter(value => !value.disabled).every(value => value.checked === true);
    const allUnChecked = this.displayData.filter(value => !value.disabled).every(value => !value.checked);
    this.allChecked = allChecked;
    this.indeterminate = (!allChecked) && (!allUnChecked);
  }

  /**
   * 全选方法
   */
  checkAll(value: boolean): void {
    this.displayData.forEach(data => {
      if (!data.disabled) {
        data.checked = value;
      }
    });
    // 刷新状态
    this.refreshStatus();
  }

  /**
   * 创建项目的模态框
   */
  createProgramModal(): void {
    const modal = this.modalService.create({
      nzTitle: '创建项目',
      nzContent: AddProjectComponent,
      nzComponentParams: {
      },
      nzFooter: null
    });
    modal.afterOpen.subscribe(() => console.log('[afterOpen] emitted!'));
    modal.afterClose.subscribe(result => {
      this.getProgramPage();
      this.refreshOverview();
      this.loading = true;
      
    });
  }

  /**
   * 创建应用的模态框
   */
  addAppModal(projectUuid: string): void {
    const modal = this.modalService.create({
      nzTitle: '新增应用',
      nzContent: AddAppComponent,
      nzComponentParams: {
        projectUuid: projectUuid
      },
      nzFooter: null
    });
    modal.afterOpen.subscribe(() => console.log('[afterOpen] emitted!'));
    modal.afterClose.subscribe(result => {
      this.getProgramPage();
    });
  }
  /**
   * 创建应用后刷新 overview组件
   */
  refreshOverview() {
    this.projectService.projectCount().subscribe((appData) => {
        if (appData.status === 1) {
          this.overviewProMark = 'pro';
         console.log("over==>", appData);
         this.overviewProData = {
          projectNum: appData.data.projectNum,
          jarNum: appData.data.jarNum,
          cveNum: appData.data.cveNum,
          licenseNum: appData.data.licenseNum
        };
        }
        this.loading = false;
      });
}
  ngOnInit(): void {
    this.getProgramPage();
  }

}
